package com.hxkj.cst.chengshuitong.ui.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import com.hxkj.cst.chengshuitong.*
import com.hxkj.cst.chengshuitong.base.BaseActivity
import com.hxkj.cst.chengshuitong.base.BasePresenter
import com.hxkj.cst.chengshuitong.base.RatePaymentFragment
import com.hxkj.cst.chengshuitong.presenter.CapturePresenter
import com.hxkj.cst.chengshuitong.ui.fragment.RatePaymentFirstFragment
import com.hxkj.cst.chengshuitong.ui.fragment.RatePaymentFourthFragment
import com.hxkj.cst.chengshuitong.ui.fragment.RatePaymentSecondFragment
import com.hxkj.cst.chengshuitong.ui.fragment.RatePaymentThirdFragment
import com.hxkj.cst.chengshuitong.util.LogUtils
import com.zbar.lib.CaptureActivity
import com.zbar.lib.CaptureActivity.SCANNIN_GREQUEST_CODE

/**
 * 作者：LuoYang
 * 时间：2017/5/31:15:32
 * 邮箱：18981443691@163.com
 * 说明：管理流程
 */
class RatePaymentActivity : BaseActivity<BasePresenter>(), RatePaymentObserver {
    override fun provideContentView(): Int = R.layout.activity_rate_payment

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val dqlc = intent.getStringExtra("dqlc")

        if (dqlc == null) {
            val process = currentProcess(RatePaymentEnum.FIRST_RATE_PAYMENT)
            if (process != null)
                loadFragment(process)
        } else {
            LogUtils.e(dqlc)
            when (dqlc) {
                "first" -> currentProcess(RatePaymentEnum.FIRST_RATE_PAYMENT)?.let { loadFragment(it) }

                "second" -> currentProcess(RatePaymentEnum.SECOND_RATE_PAYMENT)?.let { loadFragment(it) }

                "third" -> currentProcess(RatePaymentEnum.THIRD_RATE_PAYMENT)?.let { loadFragment(it) }

                "fourth" -> currentProcess(RatePaymentEnum.FOURTH_RATE_PAYMENT)?.let { loadFragment(it) }
            }
        }
    }

    var fragmentManager: FragmentManager? = null

    fun loadFragment(fragment: Fragment) {
        if (fragmentManager == null)
            fragmentManager = supportFragmentManager
        val fragmentTransaction = fragmentManager?.beginTransaction()
        fragmentTransaction?.replace(R.id.arp_fragment_contain, fragment)
        fragmentTransaction?.commit()
    }

    /*
    * 返回当前流程界面
    * */
    fun currentProcess(ratePaymentEnum: RatePaymentEnum): RatePaymentFragment? {
        var ratepaymentfragment: RatePaymentFragment? = null
        when (ratePaymentEnum) {
            RatePaymentEnum.FIRST_RATE_PAYMENT -> ratepaymentfragment = RatePaymentFirstFragment()
            RatePaymentEnum.SECOND_RATE_PAYMENT -> ratepaymentfragment = RatePaymentSecondFragment()
            RatePaymentEnum.THIRD_RATE_PAYMENT -> ratepaymentfragment = RatePaymentThirdFragment()
            RatePaymentEnum.FOURTH_RATE_PAYMENT -> ratepaymentfragment = RatePaymentFourthFragment()
            RatePaymentEnum.CAPTURE_SBB -> skipToCapture(CapturePresenter.current_capture_sbb)
            RatePaymentEnum.CAPTURE_HGZ -> skipToCapture(CapturePresenter.current_capture_hgz)
            RatePaymentEnum.RATE_PAYMENT_FINISH -> finish()
        }
        ratepaymentfragment?.registerObserver(this)
        return ratepaymentfragment
    }

    fun skipToCapture(location: Int) {
        setDataToShared("location", location)
        startActivityForResult(Intent(this, CaptureActivity::class.java), SCANNIN_GREQUEST_CODE)
    }

    override fun update(ratePaymentEnum: RatePaymentEnum) {
        val process = currentProcess(ratePaymentEnum)
        if (process != null)
            loadFragment(process)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SCANNIN_GREQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                val result = data?.getStringExtra("QR_CODE")
                val bundle = Bundle();
                toast(result.toString())
                bundle.putString("SCANNER_RESULT", result)
                val ratePaymentFragment = currentProcess(RatePaymentEnum.SECOND_RATE_PAYMENT)
                ratePaymentFragment?.let {
                    it.arguments = bundle
                    loadFragment(it)
                }
            } else {
                toast("无法获取扫码结果")
            }
        }
    }
}